package com.cloud.bssp.admin.wanda.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.bssp.admin.wanda.entity.EmployeeDO;
import com.cloud.bssp.admin.wanda.dto.EmployeeDTO;
import com.cloud.bssp.admin.wanda.service.EmployeeService;
import com.cloud.bssp.admin.wanda.convert.EmployeeDoConvert;
import com.cloud.bssp.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Description: 员工表
 * Create Date: 2021-07-08T15:36:33.901
 * Modified By：<br>
 * Modified Date：<br>
 * Why & What is modified：<br>
 *
 * @author weirx
 * @version 1.0
 */
@Api(tags = "员工表")
@RestController
@RequestMapping("/employee")
@AllArgsConstructor
public class EmployeeController {

    /**
     * EmployeeService
     */
    private EmployeeService employeeService;

    /**
     * 分页列表
     * @param employeeDTO
     * @return
     */
    @ApiOperation(value = "分页列表")
    @PostMapping("/pageList")
    public R pageList(@RequestBody EmployeeDTO employeeDTO) {
        QueryWrapper<EmployeeDO> queryWrapper = new QueryWrapper<>(EmployeeDoConvert.dtoToDo(employeeDTO));
        Page<EmployeeDO> page = new Page<>(employeeDTO.getPage(), employeeDTO.getLimit());
        Page<EmployeeDO> pageList = employeeService.page(page, queryWrapper);
        return R.success(EmployeeDoConvert.pageConvert(pageList));
    }

    /**
     * list列表
     * @param employeeDTO
     * @return
     */
    @ApiOperation(value = "list列表")
    @PostMapping("/list")
    public R list(@RequestBody EmployeeDTO employeeDTO) {
        QueryWrapper<EmployeeDO> queryWrapper = new QueryWrapper<>(EmployeeDoConvert.dtoToDo(employeeDTO));
        List<EmployeeDO> employeeList = employeeService.list(queryWrapper);
        return R.success(EmployeeDoConvert.listConvert(employeeList));
    }

    /**
     * 根据主键查询
     * @param id
     * @return
     */
    @ApiOperation(value = "根据主键查询")
    @GetMapping("/info/getById")
    public R info(@RequestParam("id")Long id) {
            EmployeeDO employee = employeeService.getById(id);
        return R.success(EmployeeDoConvert.doToDto(employee));
    }

    /**
     * 新增
     * @param employeeDTO
     * @return
     */
    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public R save(@RequestBody EmployeeDTO employeeDTO) {
        boolean flag = employeeService.save(EmployeeDoConvert.dtoToDo(employeeDTO));
        if (flag) {
            return R.success();
        }
        return R.failed();
    }

    /**
     * 更新
     * @param employeeDTO
     * @return
     */
    @ApiOperation(value = "更新")
    @PostMapping("/update")
    public R update(@RequestBody EmployeeDTO employeeDTO) {
        boolean flag = employeeService.updateById(EmployeeDoConvert.dtoToDo(employeeDTO));
        if (flag) {
            return R.success();
        }
        return R.failed();
    }
}